﻿using System;
using System.Collections.Generic;
using System.DrawingCore;
using System.DrawingCore.Drawing2D;
using System.DrawingCore.Imaging;
using System.IO;
using System.Security.Cryptography;
using System.Text;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace AgencyManagerSystem.Common
{
    public static class Util
    {
        public static string ToSHA1(string str)
        {
            var buffer = Encoding.UTF8.GetBytes(str);
            var data = SHA1.Create().ComputeHash(buffer);

            var sb = new StringBuilder();
            foreach (var t in data)
            {
                sb.Append(t.ToString("X2"));
            }
            return sb.ToString();
        }
        public static string GetTimeFormat()
        {
            return "yyyy-MM-dd HH:mm:ss";
        }

        public static string GetDateFormat()
        {
            return "yyyy-MM-dd";
        }

        public static string Now()
        {
            return DateTime.Now.ToString(GetTimeFormat());
        }
        public static string Today()
        {
            return DateTime.Now.ToString(GetDateFormat());
        }
        public static string ConvertToJson(Object obj)
        {
            try
            {
                return JsonConvert.SerializeObject(obj);
            }
            catch
            {
                return "{}";
            }
        }
        public static T ConvertObjectFromJson<T>(string json) 
        {
            try
            {
                return JsonConvert.DeserializeObject<T>(json);
            }
            catch(Exception ex)
            {
                LogHelper.Warn(typeof(Util), String.Format("Can't resolv to {0} from string {1}", typeof(T), json));
                return default(T);
            }
        }
        public static IEnumerable<T> ConvertArrayFromJson<T>(string json) 
        {
            IList<T> ls = new List<T>();
            try
            {
                JArray array = JsonConvert.DeserializeObject(json) as JArray;
                foreach(JToken token in array)
                {
                    ls.Add(token.ToObject<T>());
                }
                return ls;
            }
            catch(Exception ex)
            {
                LogHelper.Warn(typeof(Util), String.Format("Can't resolv to {0} from string {1}", typeof(T), json));
                return ls;
            }
        }
        public static void CreateThumbnail(string path,byte[] imgByte)
        {
            const int size = 150;
            Stream stream = new MemoryStream();
            stream.Write(imgByte,0,imgByte.Length);
            using (var image = new Bitmap(Image.FromStream(stream)))
            {
                int width, height;
                if (image.Width > image.Height)
                {
                    width = size;
                    height = Convert.ToInt32(image.Height * size / (double)image.Width);
                }
                else
                {
                    width = Convert.ToInt32(image.Width * size / (double)image.Height);
                    height = size;
                }
                var resized = new Bitmap(width, height);
                using (var graphics = Graphics.FromImage(resized))
                {
                    graphics.CompositingQuality = CompositingQuality.HighSpeed;
                    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    graphics.CompositingMode = CompositingMode.SourceCopy;
                    graphics.DrawImage(image, 0, 0, width, height);
                    resized.Save(path);
                }
            }
        }
    }
}
